import axios from 'axios'
import { showToast } from 'vant'

const http = axios.create()
http.defaults.timeout = 10000

// 请求拦截
http.interceptors.request.use(
  async (config: any) => {
    // config.headers.xxx = xxx
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  },
)

// 响应拦截器
http.interceptors.response.use(
  (response) => {
    const res = response.data

    if (res?.msg)
      showToast(res.msg)

    if (res.state !== 'ok')
      return Promise.reject(res)
    return res
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default http
